package org.example.netty;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 王博
 */
@Slf4j
public class EmbeddedDemo {
    public static void main(String[] args) {
        EmbeddedChannel embeddedChannel = new EmbeddedChannel(
                new LengthFieldBasedFrameDecoder(200, 0, 4, 0, 4),
                new LoggingHandler(LogLevel.INFO)
        );
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        extracted("AAAAAAAAAAAA", buf);
        embeddedChannel.writeInbound(buf);
        extracted("BBBBBBB", buf);
        embeddedChannel.writeInbound(buf);
    }

    private static void extracted(String str, ByteBuf buf) {
        byte[] bytes = str.getBytes();
        buf.writeInt(bytes.length);
        buf.writeBytes(bytes);
    }
}
